package hostsystem;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ProfileHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
		HttpSession session = req.getSession();
		if (session.getAttribute("islogin") == null) {
			resp.sendRedirect("login.html");
			return;
		}

		String account_id = (String) session.getAttribute("accountid");
		Connection con = hostdb.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = con.prepareStatement("select * from users where account_id=?");
			ps.setString(1, account_id);
			rs = ps.executeQuery();
			// System.out.println(account_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (rs.next()) {
				req.setAttribute("name", rs.getString("name"));
				req.setAttribute("type", rs.getInt("type"));
				ps.close();
				con.close();
				req.getRequestDispatcher("/changeaccount.jsp").forward(req,
						resp);
				return;
			} else {
				System.out.println("system error!");
			}
			ps.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.sendRedirect("login.html");
	}
}